# -*- coding: utf-8 -*-
# @Time : 2025/2/28 11:09
# @Author : debbyszhang
# @File : relay_list_page.py
# @desc :
import time
from typing import List

from playwright.sync_api import Page

from libs.base_page import BasePage


class RelayListPage(BasePage):

    def __init__(self, page: Page):
        super().__init__(page)
        # self.page.screenshot(path="./sssss111.png")
        self.page.get_by_role("link", name="中继网关").click()
        # self.page.screenshot(path="./sssss.png")

    def logout(self):
        self.page.locator(".tea-dropdown").nth(1).hover()
        self.page.get_by_text('退出登录').click()

    def flip_over(self):
        self.page.get_by_role("main").locator("a").nth(3).click()
        self.page.get_by_role("main").locator("a").first.click()
        self.page.get_by_role("main").locator("a").nth(2).click()
        self.page.get_by_role("main").locator("a").nth(1).click()

    def get_token(self):
        self.click('获取token')
        self.click('复制')

    def search_gateway(self, content):
        """
        搜索功能
        :return:
        """
        self.click_fill("请输入网关名称", content)
        self.page.get_by_placeholder("请输入网关名称").press("Enter")
        self.click('搜索')
        time.sleep(1)

    def new_gateway(self, gateway_id, gateway_name):
        self.click("创建网关")
        self.click_fill("请输入中继网关ID", gateway_id)
        self.click_fill("请输入中继网关名称", gateway_name)
        self.click_fill("请输入合约名", 'batch')
        self.click_fill("请输入跨链合约名", 'contract')
        self.click_fill("请输入IP：端口或域名", '192.168.12.11:12')
        self.click_fill("请输入监听端口信息", '1211')
        self.click_fill("请输入授权码私钥", 'abcd1234')
        self.click("提交")

    def new_event(self, row_number, target_gateway_id, target_chainrid, target_contract, try_method):
        self.page.get_by_role("button", name="事件配置").nth(row_number).click()
        self.click_fill("请输入合约名", 'crossquery')
        self.click_text("跨链执行")
        self.click_fill("请输入目标网关ID", target_gateway_id)
        self.click_fill("请输入目标链资源ID", target_chainrid)
        self.click_fill("请输入目标链合约名", target_contract)
        self.click_fill("请输入方法名", try_method)
        self.click('确定')

    def apply(self, row_number, relay_name, approve):
        self.page.get_by_role("button", name="接入授权").nth(row_number).click()
        self.click("授权")
        if approve == "通过":
            self.check("通过")
            self.click("确定")
        elif approve == "驳回":
            self.page.locator("label").filter(has_text="驳回").locator("i").click()
            self.click_fill("请输入驳回原因", '不通过')
            self.click("确定")
        t = self.page.locator("#tea-message-root div").inner_text()
        print(t)  # '操作成功'
        return t

    def get_gateway_list_data(self, title, num=0):
        headers = self.page.locator("table thead tr th")
        headers_text = [header.text_content() for header in headers.all()]
        print("表头:", headers_text)

        rows = self.page.locator("table tbody tr")
        if rows.count() == 0:
            print("列表中没有数据")

        # 获取第一行数据
        first_row = rows.nth(num)
        cells = first_row.locator("td")
        cells_text = [cell.text_content() for cell in cells.all()]
        print(f"第{num + 1}行数据:", cells_text)

        # 提取网关名称
        value_index = headers_text.index(title)
        value = cells_text[value_index]
        print("数据:", value)

        return value

    def gateway_list(self) -> List:
        """
        跨链网关列表（点击后的展示的数据）
        :return:
        """
        self.page.get_by_role("link", name="跨链网关").click()

        data_list = self.get_gateway_list()
        return data_list

        # # 获取表头
        # headers = self.page.locator("table th").all()
        # headers = [header.text_content() for header in headers]
        #
        # # 获取所有表格行
        # rows = self.page.locator("table tbody tr").all()
        #
        # data_list = []
        # for row in rows:
        #     # 获取当前行的所有单元格
        #     cells = row.locator("td").all()
        #     cells = [cell.text_content() for cell in cells]
        #
        #     # 创建字典并添加到列表中
        #     data_dict = dict(zip(headers, cells))
        #     data_list.append(data_dict)
        #
        # return data_list

    def get_gateway_list(self) -> List:
        """
        获取跨链网关列表数据（用于搜索后获取返回结果）
        :return:
        """
        # 获取表头
        headers = self.page.locator("table th").all()
        headers = [header.text_content() for header in headers]

        # 获取所有表格行
        rows = self.page.locator("table tbody tr").all()

        data_list = []
        for row in rows:
            # 获取当前行的所有单元格
            cells = row.locator("td").all()
            cells = [cell.text_content() for cell in cells]

            # 创建字典并添加到列表中
            data_dict = dict(zip(headers, cells))
            data_list.append(data_dict)

        return data_list
